#include "login_sign.h"

int login_sign(int *sockfd, char *arg1, char *arg2, cmd_t *cmd)
{
    char buf[4096];
    printf("Do you login or sign a new user?\n");
    while (1)
    {
        printf("input <login/sign>\n");

        bzero(buf, sizeof(buf));
        ssize_t sret = read(STDIN_FILENO, buf, sizeof(buf));
        ERROR_CHECK(sret, -1, "read");
        ERROR_CHECK(sret, 0, "read");
        buf[sret - 1] = '\0';

        bzero(cmd, sizeof(cmd_t));
        int ret = commandAnalyze(buf, cmd);

        if (ret == 0)
        {
            if (cmd->cmdType == CMD_LOGIN){
                int ret = tcpClientInit(sockfd, arg1, arg2);
                ERROR_CHECK(ret, -1, "tcpClientInit");
                break;
            }
            if (cmd->cmdType == CMD_SIGN)
            {
                
                while (1)
                {
                    ret = userSign(*sockfd, cmd);
                    if (ret == 0)
                    {
                        break;
                    }
                }
            }
        }
    }

    printf("you have enter login interface.\n");
    
    while (1)
    {
        int ret = userLogin(*sockfd, cmd);
        if (ret == 0)
        {
            break;
        }
    }
    printf("please input command.\n");
    return 0;
}
